package io.sdph.controller;

import io.sdph.userprofile.ISessionManager;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * Servlet odpowiedzialny za mozliwosc ustalenia limitu kredytowego danemu uzytkownikowi  w panelu przedstawiciela handlowego (admin.jsp)
 *
 * @author MODUL FRONTU (WWW)
 */
@Controller
@RequestMapping("/admin/SetCreditLimit")
public class AdminSetCreditLimit {

    	private static final String SELECTED_USER = "users";
        private static final String CREDIT_LIMIT = "creditLimit";
        private static final String ANOTHER = "all";        

        private static final String OUTPUT_ERROR = "errors";
        private static final String CREDIT_LIMIT_EMPTY_ERROR = "adminSetCreditLimit.creditLimit.error.empty";
       
        private static final String PAGE_VIEW = "admin";

        /** Komponent pomocniczy do zarz�dzania sesj� u�ytkownika. */
        @Autowired
        ISessionManager sessionManager;
       
        //HttpServletRequest arg0, HttpServletResponse arg1
        @RequestMapping(method=RequestMethod.GET)
        public ModelAndView showPage() throws Exception {
               
                ModelAndView model = new ModelAndView(PAGE_VIEW);
                model.addObject("pageName", "creditLimit");
                return model;
        }
       
        @RequestMapping(method=RequestMethod.POST)
        public ModelAndView adminSetCreditLimit(
                		@RequestParam(SELECTED_USER) final String selectedProduct, 
                        @RequestParam(CREDIT_LIMIT) final String creditLimit                     
        				) {
               
                ModelAndView model = new ModelAndView(PAGE_VIEW);
                
                Map<String, String> errors = new HashMap();
               
                if (StringUtils.isEmpty(creditLimit)) 
                        errors.put(CREDIT_LIMIT, CREDIT_LIMIT_EMPTY_ERROR);          
               
                model.addObject(OUTPUT_ERROR, errors);
         
                model.addObject("pageName", "creditLimit");
                return model;
        }
       
}
